import time

from selenium.webdriver.common.by import By

from common.utils import Driver
from common.utils import ScreenShot


class BlogDetail:
    def __init__(self):
        self.blog_urls = Driver.find_elements(By.CLASS_NAME, "detail")
        self.url = self.blog_urls[0].get_property("href")
        Driver.get(self.url)
        Driver.implicitly_wait(20)

    # 检查博客详情页元素
    def check_ele(self):
        # 用户信息
        Driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div/img")
        Driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div/h3")
        Driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div/a")
        Driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div/div[1]")
        Driver.find_element(By.XPATH, "/html/body/div[2]/div[1]/div/div[2]")
        # 博客内容
        Driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div[1]   ")
        Driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div[2]")
        Driver.find_element(By.XPATH, "//*[@id=\"detail\"]")
        ScreenShot()

    # 测试编辑按钮
    def check_edit(self):
        edit = Driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div[4]/button[1]")
        edit.click()
        edit_url = Driver.current_url
        Driver.back()
        assert self.url != edit_url


    # 测试删除功能
    def check_del(self):
        # 所有博客链接
        blog_links = Driver.find_elements(By.CLASS_NAME, "detail")
        # 进入某个博客详情页，点击删除按钮，切换到确认窗口点击确认
        if len(blog_links) > 0:
            blog_links[0].click()
            Driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div[4]/button[2]").click()
            Driver.switch_to.alert.accept()
            ScreenShot()
